package com.sailing.xjpb.consul;

import com.ecwid.consul.v1.ConsulClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryClient;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
import org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration;
import org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * consul重试自动配置
 *
 * @author 罗志银
 * createDate 2020/8/10 17:51
 */
@Configuration
@ConditionalOnClass({
        ConsulDiscoveryClient.class,
        ConsulAutoRegistration.class,
        ConsulServiceRegistry.class,
        ConsulClient.class
})
@ConditionalOnProperty(prefix = "spring.cloud.consul", name = "enabled", havingValue = "true")
@Import({ConsulRetryProperties.class})
public class ConsulRetryAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean({ConsulRetryRegistry.class})
    public ConsulRetryRegistry consulRetryRegistry(
            ConsulDiscoveryProperties consulDiscoveryProperties,
            ConsulAutoRegistration consulAutoRegistration,
            ConsulServiceRegistry consulServiceRegistry,
            ConsulRetryProperties properties,
            ConsulClient client
    ) {
        return new ConsulRetryRegistry(
                consulDiscoveryProperties,
                consulAutoRegistration,
                consulServiceRegistry,
                properties,
                client
        );
    }
}
